package io.renren.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.bean.ExercisesQuestionInfo;
import io.renren.common.utils.R;
import io.renren.dao.SubjectDao;
import io.renren.entity.*;
import io.renren.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("subjectService")
public class SubjectServiceImpl extends ServiceImpl<SubjectDao, SubjectEntity> implements SubjectService {

    @Autowired
    private StuSubService stuSubService;
    @Autowired
    private QuestionRecordService questionRecordService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private QuestionErrorService questionErrorService;
    @Autowired
    private StudentService studentService;

    /***
     * 获取科目列表
     * @param studentId 学员id
     * @return R
     */
    @Override
    public List<Map<String, Object>> getSubjectList(Long studentId) {
        return baseMapper.getSubjectList(studentId);
    }


    /***
     * 获取练习题目信息
     * @param student 学员
     * @param subjectId 科目id
     * @param exercisesType 1顺序练习 2随机 3错题
     * @param sort 序号(只有顺序练习有)
     * @param state 是否查序号
     * @param type 针对错题练习 1为错题题目练习 2为收藏题目练习(错题练习一定要传type)
     * @return R
     */
    @Override
    public R getQuestionList(StudentEntity student, Integer subjectId, Integer exercisesType, Integer sort, Integer state, Integer type) {
        if (exercisesType == null || (exercisesType != 1 && exercisesType != 2 && exercisesType != 3)
                || (exercisesType == 2 && sort == null)
                || (sort != null && sort < 1)
                || ((exercisesType == 3 && sort == null) && (type != null && (type != 1 || type != 2)))) {
            return R.fail("参数错误");
        } else {
            //返回学员科目中间表对象
            StuSubEntity stuSub = stuSubService.stuSub(subjectId, student.getUserId());
            if (stuSub == null) {
                return R.fail("暂不能使用该科目");
            } else {
                //获取科目总题数
                Integer sumQuestionNumber = questionService.getSumQuestionNumber(subjectId);
                if (sumQuestionNumber == 0) {
                    return R.fail("该科目暂时没题目");
                } else {
                    if (exercisesType == 1) {
                        if (sort == null) {
                            //获取顺序练习序号
                            sort = questionRecordService.getSort(subjectId, stuSub.getQuestionId());
                        }
                        if (sort > sumQuestionNumber) {
                            sort = sumQuestionNumber;
                        }
                        //获取顺序练习题目
                        ExercisesQuestionInfo questionBean = questionRecordService.getSequenceQuestion(student.getUserId(), subjectId, sumQuestionNumber, sort, state);
                        if (questionBean != null && questionBean.getQuestionInfo() != null) {
                            //顺序练习 在学生_科目中间表中记录最后的问题id
                            stuSub.setQuestionId(questionBean.getQuestionInfo().getQuestionId());
                            stuSub.setLastModifyDate(new Date());
                            stuSubService.updateById(stuSub);
                        }
                        return R.ok(questionBean);
                    } else if (exercisesType == 2) {
                        //随机练习
                        ExercisesQuestionInfo questionBean = questionService.getRandomQuestion(student.getUserId(), subjectId,
                                sumQuestionNumber, sort);
                        return R.ok(questionBean);
                    } else {
                        //错题练习
                        //获取错题的数量
                        Integer errorQuestionNumber = questionErrorService
                                .getQuetionNumber(subjectId, student.getUserId(), type);
                        if (errorQuestionNumber == null) {
                            return R.fail("你没有错题");
                        } else {
                            if (sort >= errorQuestionNumber) {
                                sort = errorQuestionNumber;
                            }
                            //获取错题/收藏题目
                            ExercisesQuestionInfo errroQuestionList = questionErrorService.getErrorQuestion(student.getUserId(), subjectId, errorQuestionNumber, sort, type);
                            return R.ok(errroQuestionList);
                        }
                    }
                }
            }
        }
    }

    /***
     * 小程序
     * 获取科目列表
     * @param student 学员
     * @param subjectName 科目名称
     * @return
     */
    @Override
    public List<Map<String, Object>> getXSubjectList(StudentEntity student, String subjectName) {
        return baseMapper.getXSubjcetList(student.getUserId(), student.getSchoolId(), subjectName);
    }


    /***
     * 保存学员的答题记录
     * @param student 学员
     * @param questionId 题目id
     * @param exercisesType 练习类型，1表示顺序练习；2表示随机练习；3表示错题练习
     * @param studentAnswer 学员答案
     * @return R
     */
    @Override
    public R recordAnswer(StudentEntity student,
                          Integer questionId,
                          Integer exercisesType,
                          String studentAnswer) {
        if ((exercisesType != 1 && exercisesType != 2 && exercisesType != 3)) {
            return R.fail("参数错误");
        }
        if (exercisesType == 1) {
            //顺序练习保存答案
            questionRecordService.recordAnswer(student.getUserId(), questionId, studentAnswer);
        }
        //找出题目实体类
        QuestionEntity questionEntity = questionService.selectById(questionId);
        Map<String, Object> map = new HashMap<>();
        map.put("trueAnswer", questionEntity.getTrueAnswer());
        //题目得正确答案和学生答案做比较
        if (questionEntity.getTrueAnswer().equals(studentAnswer)) {
            map.put("isTrue", true);
            if (exercisesType == 3) {
                //查询出错题实体类
                QuestionErrorEntity questionErrorEntity = questionErrorService.selectQuestionError(student.getUserId(), questionId);
                if (questionErrorEntity != null) {
                    //如果在错题练习中把题目做对了 就删除该错题
                    questionErrorService.deleteById(questionErrorEntity.getId());
                }
            }
        } else {
            map.put("isTrue", false);
            if (exercisesType != 3) {
                //错题入库   顺序练习，随机练习中做错得题就入库到错题练习中
                questionErrorService.errorQuestionStorage(student.getUserId(), questionId);
            }
        }
        //无论做对还是做错都展现题目的讲解
        map.put("answerRemaek", questionEntity.getRemark());
        return R.ok(map);
    }


}
